import { Message } from 'element-ui'

let Util = {}
Util.install = function (Vue, options) {
  Vue.formatDateTime = Vue.prototype.$formatDateTime = function (time, str) {
    let theDate = new Date(time)
    var _year = theDate.getFullYear()
    var _month =  ("0" + (theDate.getMonth() + 1)).slice(-2)
    var _date = ("0" + theDate.getDate()).slice(-2)
    var _hour = ("0" + theDate.getHours()).slice(-2)
    var _minute = ("0" + theDate.getMinutes()).slice(-2)
    var _second = ("0" + theDate.getSeconds()).slice(-2)
    if (str === 'zh') {
      return  _year + "年" + _month + "月" + _date + "日 " + _hour + ":" + _minute + ":" + _second
    } else if (str) {
      return  _year + str + _month + str + _date + " " + _hour + ":" + _minute + ":" + _second
    }
    return  _year + "-" + _month + "-" + _date + " " + _hour + ":" + _minute + ":" + _second
  }

  Vue.formatYMD = Vue.prototype.$formatYMD = function (time, str) {
    let theDate = new Date(time)
    var _year = theDate.getFullYear()
    var _month =  ("0" + (theDate.getMonth() + 1)).slice(-2)
    var _date = ("0" + theDate.getDate()).slice(-2)
    if (str == 'zh') {
      return _year + '年' + _month + '月' + _date + '日'
    } else if (str) {
      return  _year + str + _month + str + _date
    }
    return  _year + "-" + _month + "-" + _date
  }

  Vue.promptMsg = Vue.prototype.$promptMsg = function () {
    const h = this.$createElement;
    // <div class="flex-row-center" style="height: 500px;">
    //   <img src="static/gongnengtishi.png" alt="">
    //   <span>很抱歉！此功能正在建设中，敬请期待！</span>
    // </div>
    Message({
      // duration: 0,
      showClose: true,
      dangerouslyUseHTMLString: true,
      message: h('div', {attrs: {
        'class': 'flex-row-center promp-msg'
      }}, [h('img', {attrs: {'src': 'static/gongnengtishi.png'}}),
            h('span', null, '很抱歉！此功能正在建设中，敬请期待！')])
    })
  }

  Vue.lsput = Vue.prototype.$lsput = function (key, value) {
    if (typeof value === 'object') {
      value = JSON.stringify(value)
    }
    localStorage.setItem(key, value)
  }

  Vue.lsget = Vue.prototype.$lsget = function (key) {
    let val = localStorage.getItem(key)
    try {
      return JSON.parse(val)
    } catch (e) {
      console.log(e)
      return val
    }
  }
  // 对字符串进行加密
  Vue.compileStr = Vue.prototype.$compileStr = function (code) {
    let c = String.fromCharCode(code.charCodeAt(0) + code.length)
    for (let i = 1; i < code.length; i++) {
      c += String.fromCharCode(code.charCodeAt(i) + code.charCodeAt(i - 1))
    }
    return escape(c)
  }
  // 字符串进行解密
  Vue.uncompileStr = Vue.prototype.$uncompileStr = function (code) {
    code = unescape(code)
    let c = String.fromCharCode(code.charCodeAt(0) - code.length)
    for (var i = 1; i < code.length; i++) {
      c += String.fromCharCode(code.charCodeAt(i) - c.charCodeAt(i - 1))
    }
    return c
  }
  // 通过地址栏的参数获取参数内容
  Vue.getUrlParames = Vue.prototype.$getUrlParames = function (param) {
    var query = window.location.href
    var iLen = param.length
    var iStart = query.indexOf(param)
    if (iStart === -1) {
      return ''
    }
    iStart += iLen + 1
    var iEnd = query.indexOf('&', iStart)
    if (iEnd === -1) {
      return query.substring(iStart)
    }
    return query.substring(iStart, iEnd)
  }
  // 从地址栏获取加密的参数
  Vue.getUrlCompileParames = Vue.prototype.$getUrlCompileParames = function (param) {
    var query = Vue.uncompileStr(window.location.search.slice(1, window.location.search.length))
    var iLen = param.length
    var iStart = query.indexOf(param)
    if (iStart === -1) {
      return ''
    }
    iStart += iLen + 1
    var iEnd = query.indexOf('&', iStart)
    if (iEnd === -1) {
      return query.substring(iStart)
    }
    return query.substring(iStart, iEnd)
  }

  // 从地址栏获取加密的参数 (uuid长度， 进制，2，8， 10， 16)
  Vue.uuid = Vue.prototype.$uuid = function (len, radix) {
     var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
     var uuid = [], i;
     radix = radix || chars.length;

     if (len) {
       // Compact form
       for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
     } else {
       // rfc4122, version 4 form
       var r;

       // rfc4122 requires these characters
       uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
       uuid[14] = '4';

       // Fill in random data.  At i==19 set the high bits of clock sequence as
       // per rfc4122, sec. 4.1.5
       for (i = 0; i < 36; i++) {
         if (!uuid[i]) {
           r = 0 | Math.random()*16;
           uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
         }
       }
     }

     return uuid.join('');
  }

  Vue.html2str = Vue.prototype.$html2str = function (html) {
    return html.replace(/<(style|script|iframe)[^>]*?>[\s\S]+?<\/\1\s*>/gi,'').replace(/<[^>]+?>/g,'').replace(/\s+/g,' ').replace(/ /g,' ').replace(/>/g,' ');
  }

}

export default Util
